<?php

require_once dirname(__FILE__).'/../lib/private_messagesGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/private_messagesGeneratorHelper.class.php';

/**
 * private_messages actions.
 *
 * @package    sf_sandbox
 * @subpackage private_messages
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class private_messagesActions extends autoPrivate_messagesActions
{
   public function executeNew(sfWebRequest $request)
  {
    $this->form = $this->configuration->getForm();
    $this->private_messages = $this->form->getObject();
  }
    public function executeShow(sfWebRequest $request)
  {
   $this->user_id = $user_id = $this->getUser()->getGuardUser()->getId(); 
   $q = Doctrine_Query::create()
  ->from('PrivateMessages pm')
  ->where('pm.sender_id = ?', $user_id)
  ->orWhere('pm.receiver_id = ?',$user_id)
  ->orderBy('pm.id DESC');
   $this->messages = $q->fetchArray();
   
   // Flag Viewed
   /* $q = Doctrine_Query::create()
        ->update('PrivateMessages pm')
        ->set('pm.is_viewed','?','1')
        ->where('pm.sender_id = ?', $user_id)
        ->orWhere('pm.receiver_id = ?',$user_id)
        ->execute();*/
  }
  public function executeShowFullMessage(sfWebRequest $request)
  {
   $this->user_id = $user_id = $this->getUser()->getGuardUser()->getId(); 
   $id = $request->getParameter('msg_id');
   $q = Doctrine_Query::create()
  ->from('PrivateMessages pm')
  ->where('pm.id = ?', $id);
   $message = $q->fetchOne();
   
   if($message['sender_id'] == $user_id || $message['receiver_id'] == $user_id ){
       // Flag Viewed
    $q = Doctrine_Query::create()
        ->update('PrivateMessages pm')
        ->set('pm.is_viewed','?','1')
        ->where('pm.id = ?', $id)
        ->execute();
        return $this->renderText($message['body']);
   }
   $this->renderText('You can only edit your messages!.');
  }
  public function executeAdd(sfWebRequest $request)
  {
      $text = $request->getParameter('private_messages_body');
      if(empty ($text)){
          $this->error = I18nUtilities::__("Please enter the private messages body.");
          return $this->renderText($this->error);
      }
      $obj = new PrivateMessages();
      $obj->setSenderId($this->getUser()->getGuardUser()->getId());
      $obj->setReceiverId($request->getParameter('receiver_id'));
      $obj->setBody($text);
      $obj->save();
      $this->error = I18nUtilities::__("your private messages added successfuly.");
      return $this->renderText($this->error);
  }
  public function executeEdit(sfWebRequest $request)
  {
      $this->user_id = $user_id = $this->getUser()->getGuardUser()->getId(); 
    $this->private_messages = $this->getRoute()->getObject();
    if($this->private_messages->getReceiverId() == $user_id ){
        $this->private_messages->setIsViewed(1);
    }
    $this->form = $this->configuration->getForm($this->private_messages);
  }
  public function executeUpdate(sfWebRequest $request)
  {
    $this->user_id = $user_id = $this->getUser()->getGuardUser()->getId(); 
    $this->private_messages = $this->getRoute()->getObject();
    if($this->private_messages->getReceiverId() == $user_id ){
        $this->private_messages->setIsViewed(1);
    }
    $this->form = $this->configuration->getForm($this->private_messages);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }
  public function executeCreate(sfWebRequest $request)
  {
    $this->user_id = $user_id = $this->getUser()->getGuardUser()->getId(); 
    $this->form = $this->configuration->getForm();
    $this->private_messages = $this->form->getObject();
    $this->private_messages->setSenderId($user_id);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }
  public function executeList_reply(sfWebRequest $request)
  {
    $this->private_messages = PrivateMessagesTable::getInstance()->find($request->getParameter('id'));
    if($this->private_messages){
        $this->private_messages->setIsViewed(1);
        $this->private_messages->save();
    }else{
     $this->forward('private_messages', 'index');
    }
   $this->user_id = $user_id = $this->getUser()->getGuardUser()->getId(); 
   $q = Doctrine_Query::create()
    ->from('PrivateMessages pm')
    ->where('pm.sender_id = ?', $this->private_messages->getSenderId())
    ->orWhere('pm.receiver_id = ?',$this->private_messages->getSenderId())
    ->orderBy('pm.id DESC');
   $this->messages = $q->fetchArray();
   
    //$this->private_messages = $this->getRoute()->getObject();
   // $this->form = new PrivateMessagesForm();
    //$this->processForm($request, $this->form);
    //$this->setTemplate('edit');
  }
}